﻿using MachineVision.Core;
using MachineVision.Extensions;
using MachineVision.Models;
using MachineVision.Services;
using MachineVision.Shared.Events;
using MachineVision.Shared.Services;
using MachineVision.Shared.Services.Tables;
using MaterialDesignThemes.Wpf;
using Prism.Commands;
using Prism.Events;
using Prism.Regions;
using System.Windows.Media;



namespace MachineVision.ViewsModels
{
    public class MainViewModel : NavigationViewModel
    {
        public INavigationMenuService NavigationMenuService { get; }
        public MainViewModel(INavigationMenuService navigationMenuService, IRegionManager manager, IEventAggregator aggregator, ISettingService settingService)
        {
            this.NavigationMenuService = navigationMenuService;
            this.manager = manager;
            this.settingService = settingService;
            this.NavigateCommand = new DelegateCommand<NavigationItem>(Navigate);
            //订阅语言更改事件(参数为事件处理器)
            aggregator.GetEvent<LanguageEventBus>().Subscribe(LanguageChanged);
        }

        private bool isTopDrawerOpen;
        private readonly IRegionManager manager;
        private readonly ISettingService settingService;

        #region 设置顶部工具类展开状态
        /// <summary>
        /// 顶部工具类展开状态
        /// </summary>
        public bool IsTopDrawerOpen
        {
            get { return isTopDrawerOpen; }
            set 
            { 
                isTopDrawerOpen = value;
                RaisePropertyChanged();
            }
        }

        public DelegateCommand<NavigationItem> NavigateCommand { get;  set; }

        public void Navigate(NavigationItem item)
        {
            if (item == null)
            {
                return;
            }

            if (item.Name.Equals("全部")|| item.Name.Equals("All"))
            {
                IsTopDrawerOpen = true;
                return;
            }

            IsTopDrawerOpen = false;

            NavigatePage(item.PageName);
        }
        #endregion

        /// <summary>
        /// 重写的导航方法
        /// </summary>
        /// <param name="navigationContext"></param>
        public override async void OnNavigatedTo(NavigationContext navigationContext)
        {
            NavigationMenuService.InitMenus();
            NavigatePage("DashboardView");
            await ApplySettingAsync();

            base.OnNavigatedTo(navigationContext);
        }

        private void NavigatePage(string pageName)
        {
            //在 MainViewRegion区域 导航到DashboardView中 并且设置回调函数
            manager.Regions["MainViewRegion"].RequestNavigate(pageName, back =>
            {
                //回调函数
                //if (!(bool)back.Result) 这行代码检查导航是否成功。
                if (!(bool)back.Result)
                {
                    //如果导航失败，back.Error 属性将包含一个描述失败原因的异常对象。通过 System.Diagnostics.Debug.WriteLine(back.Error.Message); 语句，异常信息被写入到调试输出窗口。
                    System.Diagnostics.Debug.WriteLine(back.Error.Message);
                }
            });
        }

        /// <summary>
        /// 语言更改
        /// </summary>
        /// <param name="status"></param>
        private void LanguageChanged(bool status)
        {
            NavigationMenuService.RefreshMenus();
        }

        /// <summary>
        /// 应用系统默认设置
        /// </summary>
        /// <returns></returns>
        private async Task ApplySettingAsync()
        {
            //获取保存的Setting数据
            Setting setting = await settingService.GetSettingAsync();
            if (setting != null)
            {
                //setting.Language表示保存的数据使用的语言，进行语言跟新
                LanguageHelper.SetLangeuage(setting.Language);
                //调用LanguageChanged方法更改标题的语言
                LanguageChanged(true);

                //设置主题颜色
                SettingViewModel.ModifyTheme(theme => theme.SetBaseTheme(setting.SkinName.Equals("True") ? Theme.Dark : Theme.Light));

                if (!string.IsNullOrWhiteSpace(setting.SkinColor))
                {
                    var color = ColorConverter.ConvertFromString(setting.SkinColor);
                    SettingViewModel.ChangeHue(color);
                }
            }
        }
    }
}
